package cn.wolfcode.wolfcodemall.controller.fore;

import cn.wolfcode.wolfcodemall.domain.Address;
import cn.wolfcode.wolfcodemall.domain.Category;
import cn.wolfcode.wolfcodemall.domain.Product;
import cn.wolfcode.wolfcodemall.domain.User;
import cn.wolfcode.wolfcodemall.service.AddressService;
import cn.wolfcode.wolfcodemall.service.CategoryService;
import cn.wolfcode.wolfcodemall.service.ProductService;
import cn.wolfcode.wolfcodemall.service.UserService;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 商城主页相关请求
 */
@Controller
public class HomeController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private ProductService productService;
    @Autowired
    private AddressService addressService;
    @Autowired
    private UserService userService;
    //主页面
    @RequestMapping("/")
    public String homePage(Model model){
        //查询所有的分类信息
        List<Category> categoryList = categoryService.findAll();
        //根据分类信息查询商品信息（热卖、促销、最新）    8个
        for (Category category : categoryList) {
            List<Product> products = productService.selectHomeProductByCategoryId(category.getCategoryId());
            //将查询到的商品信息存储到分类对象中
            category.setProductList(products);
        }
        //将分类信息带到页面中显示
        model.addAttribute("categoryList",categoryList);
        return "/fore/homePage";
    }

    /**
     * 查询商品信息
     * @param productName
     * @param model
     * @return
     */
    @RequestMapping("/product")
    public String product(@RequestParam(value = "productName", required = false) String productName, Model model,
                          @RequestParam(value = "categoryId", required = false) Integer categoryId) {
        List<Product> productList;

        if (!StringUtils.hasText(productName)) {
            productList = productService.selectByCategoryId(categoryId);
        } else {
            productList = productService.selectByUser(productName);
        }
        model.addAttribute("productList", productList);
        return "/fore/productListPage";
    }

    @RequestMapping("/tmall/product/{current}/{size}")
    public String product(@RequestParam Map<String, Object> map,
                          @PathVariable("current") Integer current,
                          @PathVariable("size") Integer size,
                          Model model) {
        System.out.println(map);

        String orderBy = (String) map.get("orderBy");
        String isDesc = (String) map.get("isDesc");

        List<Product> productList = productService.selectBySort(orderBy,  Boolean.parseBoolean(isDesc) ? "DESC" : "");

        model.addAttribute("productList", productList);
        model.addAttribute("orderBy", orderBy);
        model.addAttribute("isDesc", isDesc);
        return "/fore/productListPage";
    }

    @RequestMapping("/userDetails")
    public String userDetails(Model model){
        //查询所有的省份信息
        List<Address> addressList = addressService.findRoot();
        //根据省份id查询市级信息
        List<Address> cityList = addressService.findList(addressList.get(0).getAddressAreaid());
        //根据市的id查询县区信息
        List<Address> districtList = addressService.findList(cityList.get(0).getAddressAreaid());

        model.addAttribute("addressList",addressList);
        model.addAttribute("cityList",cityList);
        model.addAttribute("districtList",districtList);

        return "/fore/userDetails";
    }

    @RequestMapping("/user/update")
    public String userUpdate(User user, HttpSession session){
        System.out.println(user);

        int update = userService.updateByPrimaryKey(user);

        if (update == 1) {
            session.setAttribute("user", user);
            return "redirect:/";
        }
        else return "/fore/userDetails";
    }


}
